package com.io;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @author: csh
 * @Date: 2022/5/31 23:02
 * @Description: bio模型是阻塞，read和wirter都是阻塞的。
 */
public class ServerHandler implements Runnable {

    private Socket socket;

    public ServerHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        InputStream in = null;
        OutputStream out = null;
        try {
            byte[] arr = new byte[1024];
            in = socket.getInputStream();
            in.read(arr);
            System.out.println("服务器接收到的信息是：" + new String(arr));
            out = socket.getOutputStream();
            out.write("hello client".getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
